package com.koron.tool.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.annocation.CasUser;
import com.koron.tool.bean.ToolBean;
import com.koron.tool.bean.ToolGroupBean;
import com.koron.tool.bean.ToolQuery;
import com.koron.tool.service.ToolAccountsManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 工器具台账管理{@code ToolManagerController} 控制层
 *
 * <p>主要提供：新增、删除、修改、查询</p>
 *
 * @Author liaodf
 * @Date 2021/10/20
 */
@Slf4j
@Api(tags = "工器具台账管理")
@RestController
@RequestMapping("/toolAccounts")
public class ToolAccountsManagerController {

    @Autowired
    private ToolAccountsManagerService service;

    /**
     * 新增组织数据
     *
     * @param user 当前用户
     * @param bean
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "新增组织")
    @PostMapping("/addGroup")
    public Response addGroup(@CasUser @ApiIgnore EamUser user, @RequestBody ToolGroupBean bean) {
        return ADOConnection.runTask(user.getCurrDs(), service, "addGroup", Response.class, user, bean);
    }

    /**
     * 获取组织详情数据
     *
     * @param user 当前用户
     * @param id
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "获取组织详情")
    @GetMapping("/getGroup")
    public Response getGroup(@CasUser @ApiIgnore EamUser user, String id) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getGroup", Response.class, id);
    }

    /**
     * 编辑组织数据
     *
     * @param user 当前用户
     * @param bean
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "编辑组织")
    @PostMapping("/updateGroup")
    public Response updateGroup(@CasUser @ApiIgnore EamUser user, @RequestBody ToolGroupBean bean) {
        return ADOConnection.runTask(user.getCurrDs(), service, "updateGroup", Response.class,user, bean);
    }


    /**
     * 获取组织当前排序
     *
     * @param user 当前用户
     * @param
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "获取组织当前排序")
    @GetMapping("/getGroupOrder")
    public Response getGroupOrder(@CasUser @ApiIgnore EamUser user,String parent) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getGroupOrder", Response.class, user , parent);
    }

    /**
     * 删除组织
     *
     * @param user 当前用户
     * @param id
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "删除组织")
    @DeleteMapping("/deleteGroup")
    public Response deleteGroup(@CasUser @ApiIgnore EamUser user, String id) {
        return ADOConnection.runTask(user.getCurrDs(), service, "deleteGroup", Response.class, id);
    }


    /**
     * 新增工器具数据
     *
     * @param user 当前用户
     * @param bean
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "新增工器具")
    @PostMapping("/addTool")
    public Response addTool(@CasUser @ApiIgnore EamUser user, @RequestBody ToolBean bean) {
        return ADOConnection.runTask(user.getCurrDs(), service, "addTool", Response.class, user, bean);
    }

    /**
     * 获取工器具当前排序
     *
     * @param user 当前用户
     * @param
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "获取工器具当前排序")
    @GetMapping("/getToolOrder")
    public Response getToolOrder(@CasUser @ApiIgnore EamUser user) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getToolOrder", Response.class, user);
    }

    /**
     * 修改工器具数据
     *
     * @param user 当前用户
     * @param bean
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "修改工器具")
    @PostMapping("/updateTool")
    public Response updateTool(@CasUser @ApiIgnore EamUser user, @RequestBody ToolBean bean) {
        return ADOConnection.runTask(user.getCurrDs(), service, "updateTool", Response.class, user, bean);
    }

    /**
     * 获取工器具详情数据
     *
     * @param user 当前用户
     * @param id
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "获取工器具详情数据")
    @GetMapping("/getTool")
    public Response getTool(@CasUser @ApiIgnore EamUser user,String id) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getTool", Response.class, user, id);
    }

    /**
     * 删除工器具数据
     *
     * @param user 当前用户
     * @param ids
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "删除工器具")
    @DeleteMapping("/deleteTool")
    public Response deleteTool(@CasUser @ApiIgnore EamUser user,String ids) {
        return ADOConnection.runTask(user.getCurrDs(), service, "deleteTool", Response.class, user, ids);
    }


    /**
     * 手动报废工器具
     *
     * @param user 当前用户
     * @param id
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "手动报废工器具")
    @GetMapping("/scrapTool")
    public Response scrapTool(@CasUser @ApiIgnore EamUser user,String id) {
        return ADOConnection.runTask(user.getCurrDs(), service, "scrapTool", Response.class, user, id);
    }

    /**
     * 获取组织数据
     *
     * @param user 当前用户
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "查询当前组织下的组织或者工器具")
    @GetMapping("/getList")
    public Response getList(@CasUser @ApiIgnore EamUser user, ToolQuery query) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getList", Response.class, query);
    }

    @ApiOperation(value = "导出工器具")
    @GetMapping("/exportTool")
    public void exportTool(@CasUser @ApiIgnore EamUser user, ToolQuery query, HttpServletResponse response) {
        try {
            query.setPage(0);
            query.setPageSize(Integer.MAX_VALUE);
            Map map = (Map) ADOConnection.runTask(user.getCurrDs(), service, "getList", Response.class, query).getData();
            List<ToolBean> toolList = (List<ToolBean>) ((PageInfo) map.get("tool")).getList();
            toolList = toolList != null ? toolList : new ArrayList<>();
            // 模板文件
            // jar内部的文件只能通过流的方式获取
            // File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "excel/inventoryReportTemplate.xlsx");
            ClassPathResource resource = new ClassPathResource("excel/toolListTemplate.xlsx");

            // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
            // xls
            // response.setContentType("application/vnd.ms-excel");
            // xlsx
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("工器具列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

            // 必须加 ToolBean.class，因为需要读取 ExcelContentProperty
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(),  ToolBean.class)
                    // 对所有String类型字段都会生效
                    // .registerConverter(new InventoryStatusConvertor())
                    .withTemplate(resource.getInputStream())
                    .build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            excelWriter.fill(toolList, writeSheet);

            // 千万别忘记关闭流
            excelWriter.finish();

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 获取组织下的库房
     * @param user 当前用户
     *
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "查询当前组织下的库房")
    @GetMapping("/getChildTree")
    public Response getChildTree(@CasUser @ApiIgnore EamUser user,String id) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getChildTree", Response.class,id);
    }

    /**
     * 获取组织下的库房或下属数据
     *
     * @param user 当前用户
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "查询当前组织下的库房或下属数据")
    @GetMapping("/getChildGroup")
    public Response getChildGroup(@CasUser @ApiIgnore EamUser user,String id) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getChildGroup", Response.class,id);
    }

    /**
     * 获取当前组织所属库房
     *
     * @param user 当前用户
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "获取当前组织所属库房")
    @GetMapping("/getWarehose")
    public Response getWarehose(@CasUser @ApiIgnore EamUser user,String id) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getWarehose", Response.class,id);
    }


    /**
     * 获取当前组织所属单位
     *
     * @param user 当前用户
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "获取当前组织所属单位")
    @GetMapping("/getDept")
    public Response getDept(@CasUser @ApiIgnore EamUser user,String id) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getDept", Response.class,id);
    }


    /**
     * 获取场站级组织
     *
     * @param user 当前用户
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "获取所有场站级组织")
    @GetMapping("/getAllStationGroup")
    public Response getAllStationGroup(@CasUser @ApiIgnore EamUser user) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getAllStationGroup", Response.class);
    }

    /**
     * 获取组织查询条件
     *
     * @param user 当前用户
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "获取组织查询条件")
    @GetMapping("/getGroupQuery")
    public Response getGroupQuery(@CasUser @ApiIgnore EamUser user) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getGroupQuery", Response.class);
    }
    /**
     * 获取工具查询条件
     *
     * @param user 当前用户
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "获取工具查询条件")
    @GetMapping("/getToolQuery")
    public Response getToolQuery(@CasUser @ApiIgnore EamUser user) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getToolQuery", Response.class);
    }

}
